<template>
	<view class="page tn-safe-area-inset-bottom" :style="{paddingTop: vuex_custom_bar_height + 'px'}">
		<view class="top-bg"></view>
		<dawnBar title="挂号科室"></dawnBar>
		<view class="page-container" style="padding-bottom: 140rpx;">
			<pBox v-if="!patientInfo.id"></pBox>
			<view class="tn-bg-white dawn-radius dawn-shadow tn-padding-top-xs" v-if="list.length > 0">
				<tn-index-list :activeColor="activeColor" :scrollTop="scrollTop" :indexList="indexList"
					:customBarHeight="Number.parseInt(vuex_custom_bar_height)"
					:stickyTop="Number.parseInt(vuex_custom_bar_height)">
					<view v-for="(item, index) in list" :key="index">
						<tn-index-anchor :index="item.key"></tn-index-anchor>

						<view class="list-box" @click="toDoctor(data_item)" v-for="(data_item,data_index) in item.data"
							:key="data_index">
							<view class="list-icon-text">
								<view class="list__left">
									<view class="list__left__text" style="color: black;">{{ data_item.departmentName }}
									</view>
								</view>
								<view class="list__right tn-padding-right-sm">
									<text class="" v-if="data_item.cms && data_item.cms.address">
										{{ data_item.address }}</text>
									<text class="tn-icon-right"></text>
								</view>
							</view>
						</view>
					</view>
				</tn-index-list>
			</view>

			<!-- 悬浮按钮-->
			<view class="tn-flex tn-footerfixed">
				<view class="tn-flex-1 justify-content-item tn-margin-sm tn-text-center">
					<tn-button @click="getData()" class="dawn-btn-lg dawn-btn-lg__main">
						<text class="tn-icon-search-list-fill tn-padding-right-xs"></text>
						<text>查 询</text>
					</tn-button>
				</view>
			</view>
			<view v-if="show">
				<dawnMsg :msg="msg"></dawnMsg>
			</view>
		</view>

		<!-- loading -->
		<dawnLoading :show="loading"></dawnLoading>

		<!-- 分页 -->
		<!-- <dawn-pagination ref="pagination" :current="queryData.pageNo" :total="total" @pagination="getData" /> -->

		<!-- 悬浮按钮-->
		<NavBtn></NavBtn>
	</view>
</template>

<script>
	import page_mixin from '@/mixins/page_mixin.js'
	import dayjs from '@/plugin/dayjs/dayjs.min.js';
	import * as RegisterApi from "@/api/hospital/register.js";
	import * as api from '@/api/api.js';
	import * as config from "@/api/config.js";
	import {
		toLogin
	} from '@/libs/login.js';
	import {
		mapGetters
	} from "vuex";
	const app = getApp();

	export default {
		name: 'order',
		mixins: [page_mixin],
		computed: mapGetters(['appConfig', 'uid', 'isLogin', 'userInfo', 'patientInfo']),
		data() {
			return {
				loading: false,
				show: false,
				msg: '',
				tabsIndex: 0,
				queryData: {
					pageSize: 10,
					pageNo: 1,
					patientId: '',
					patientName: ''
				},
				status: -1,
				dataText: '',
				// list: [],
				// 滚动的距离
				scrollTop: 0,
				activeColor: '#955309',
				// 索引列表
				indexList: [],
				list: []
			}
		},
		async onLoad(options) {
			await this.$onLaunched;
			console.info("query onLoad")
			if (!this.isLogin) {
				this.loading = true;
				toLogin();
				return;
			} else {
				this.$nextTick(() => {
					this.getData();
				})
			}
		},
		onPageScroll(e) {
			this.scrollTop = e.scrollTop
		},
		methods: {
			search(result) {
				if (result.code === 0) {
					this.queryData = result.data;
					this.queryData.pageNo = 1;
					this.queryData.pageSize = this.$refs.pagination.pageSize;
				}
			},
			toDoctor(item) {
				item.cms = {};
				this.$util.setStorageData(config.STORE_KEY.REG_DEPT, item)
				uni.navigateTo({
					url: '/hosp/register/doctor'
				})
				// uni.navigateTo({
				// 	url: '/hosp/register/doctor?params=' + JSON.stringify(item)
				// })
			},
			getData: function(page) {
				let that = this;
				if (!this.patientInfo.id) {
					this.show = true;
					this.msg = '请先添加就诊人！';
					return;
				}
				this.loading = true;
				this.show = false;
				this.$set(this, 'list', []);
				this.queryData.patientId = this.patientInfo.patientId;
				this.queryData.patientName = this.patientInfo.name;
				RegisterApi.getDept(this.queryData).then(res => {
					this.loading = false;
					console.table(res);
					if (res.code === 0) {
						const l = res.data || [];
						that.$set(this, 'list', l);
						if (l.length == 0) {
							this.show = true;
						}
						that.indexList = l.map(val => {
							return val.key
						})
					} else {
						this.show = true;
						this.msg = res.msg;
					}
				}).catch(err => {
					this.loading = false;
					this.show = true;
				})
			},
			formatDate: function(date) {
				return dayjs(date).format("YYYY-MM-DD");
			},
			formatDateTime: function(date) {
				return dayjs(date).format("YYYY-MM-DD HH:mm:ss");
			}
		}
	}
</script>

<style lang="scss" scoped>
	.list-box {
		padding: 0rpx 20rpx;
	}

	.list-icon-text,
	.list-image-text {
		padding: 25rpx 0rpx;
		border-bottom: 1px solid $dawn-line-color;
	}

	.list {
		font-size: $dawn-font-size-base;

		&__left {
			display: flex;
			align-items: center;
			justify-content: flex-start;

			&__icon,
			&__image {
				margin-right: 6rpx;
				font-size: $dawn-font-size-md + 2;
				font-weight: bolder;
				color: $dawn-theme-color-dark;
			}

			&__text {
				font-size: $dawn-font-size-lg;
				color: $dawn-color-grey;
				// font-weight: 300;
			}
		}

		&__content {
			padding: 40rpx 20rpx;
			margin-bottom: 10rpx;
		}

		&__right {
			display: flex;
			align-items: center;
			justify-content: flex-end;

			&__text {
				font-size: $dawn-font-size-lg;
				font-weight: bolder;
			}
		}
	}


	::v-deep .tn-index-list__sidebar {
		right: 15rpx;
	}

	::v-deep .tn-index-list__sidebar__item {
		font-size: $dawn-font-size-lg;
	}

	::v-deep .tn-index-anchor {
		font-size: $dawn-font-size-lg;
		width: 100%;
		box-sizing: border-box;
		padding: 10rpx 20rpx;
		color: $dawn-theme-color;
		font-weight: bolder;
		// line-height: 1.2;
		background-color: $dawn-page-bg;
		box-shadow: $dawn-box-shadow;
	}

	::v-deep .tn-index-anchor--active {
		color: #080808;
	}
</style>